"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const defines_1 = require("../lib/defines");
const utils_1 = require("../utils/utils");
function login(app) {
    const { socket, rooms: lRooms } = app.locals;
    const { classId, userId, username } = socket.handshake.query;
    socket.on(defines_1.RequestMethod.login, function ({ rooms, members }, cb) {
        const owner = { userId, username };
        const room = lRooms[classId];
        const __members = [];
        if ((0, utils_1.isType)(rooms, 'array')) {
            rooms.forEach(({ classId }) => {
                if (lRooms[classId]) {
                    lRooms[classId].getMembers().forEach(__members.push);
                    socket.broadcast.to(classId).emit('online', owner);
                }
            });
        }
        if ((0, utils_1.isType)(members, 'array')) {
            members.forEach(({ userId }) => {
                const member = room.members[userId];
                if (member) {
                    __members.push(member);
                    socket.to(member.socketId).emit('online', owner);
                }
            });
        }
        if (cb) {
            cb({
                status: 'ok',
                resolve: 'logged',
                members: __members
            });
        }
    });
}
exports.default = login;
